package com.bicon.botu.finance.wallet.api;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.finance.wallet.common.FinanceConstants;
import com.bicon.botu.finance.wallet.entity.PaymentCenter;
import com.bicon.botu.finance.wallet.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 支付中心
 *
 * @author tandk
 * @since 2018-05-17
 */
@RestController
@RequestMapping("/V1.0.0/api/payment-center")
public class PaymentCenterApi extends BaseController {

    @Autowired
    WalletService walletService;

    /**
     * 支付方式列表
     *
     * @return
     */
    @RequestMapping(value = "/list")
    public BaseResult list() {
        List<PaymentCenter> paymentCenters = new PaymentCenter().selectList(new EntityWrapper<PaymentCenter>()
                .setSqlSelect("id, payment, remark")
                .eq("whether_show", 0)
                .eq("logic_delete", Constants.DEL_LOGIC_NORMAL)
                .orderBy("sort", false));
        String balance = walletService.selectBalanceString(getUserId());
        for (PaymentCenter paymentCenter : paymentCenters) {
            if (FinanceConstants.PAYMENT_YE.equals(paymentCenter.getId())) {
                if (Constants.FAILURE.equals(balance)) {
                    paymentCenters.remove(paymentCenter);
                } else {
                    paymentCenter.setRemark(balance);
                }
                break;
            }
        }
        return paymentCenters.size() > 0 ? sendResult200(paymentCenters) : sendResult200();
    }
}
